package com.zkdcloud.service;

import com.zkdcloud.domain.PhoneCode;
import com.zkdcloud.domain.User;
import com.zkdcloud.repository.PhoneCodeDao;
import com.zkdcloud.repository.UserDao;
import com.zkdcloud.util.Md5Util;
import com.zkdcloud.util.RandomUtil;
import com.zkdcloud.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户service
 *
 * @author zk123
 * @version 2018/5/22
 */
@Service
public class UserService {
    /**
     * userDao
     */
    @Autowired
    private UserDao userDao;

    @Autowired
    private PhoneCodeDao phoneCodeDao;
    /**
     * 根据用户名和密码直查用户
     *
     * @param username un
     * @param password psd
     * @return user
     */
    public User getUserByUsernameAndPassword(String username,String password){
        List<User> userList = userDao.getByUsernameAndPwd(username,password);
        return userList == null || userList.isEmpty() ? null : userList.get(0);
    }

    /**
     * 根据用户名和密码(m5加密)
     *
     * @param username u
     * @param password p
     * @return user
     */
    public User getUserByUsernameAndPasswordWithEncodePwd(String username,String password){
        password = Md5Util.encoding(password);
        return getUserByUsernameAndPassword(username,password);
    }
    /**
     * 根据手机号和密码(m5加密)
     *
     * @param photo u
     * @param password p
     * @return user
     */
    public User getUserByPhotoNumAndPasswordWithEncodePwd(String photo,String password){
        password = Md5Util.encoding(password);
        List<User> userList = userDao.getByPhotoNumAndPwd(photo,password);
        return userList == null || userList.isEmpty() ? null : userList.get(0);
    }

    /**
     * add user
     *
     * @param user user
     */
    public void addUser(User user){
        user.setCreateTime(System.currentTimeMillis());
        user.setLastAddressId(null);
        user.setId(RandomUtil.getUUID());
        user.setPassword(Md5Util.encoding(user.getPassword()));
        if(StringUtils.isEmpty(user.getUsername())){
            user.setUsername(user.getMobile() + "#用户");
        }
        user.setAvatarUrl("http://img1.imgtn.bdimg.com/it/u=4204304719,3886203046&fm=27&gp=0.jpg");
        userDao.add(user);
    }

    /**
     * 更新user
     *
     * @param user user
     */
    public void updateUser(User user){
        userDao.updateUser(user);
    }

    /**
     * 根据id获取user
     *
     * @param id id
     * @return user
     */
    public User getById(String id){
        return userDao.getById(id);
    }

    public void sendPhoto(String mobile) throws Exception {
        List<User> userList = userDao.getByPhotoNum(mobile);
        if(userList != null && !userList.isEmpty()){
            throw new Exception("该手机号已存在");
        }
        String code = RandomUtil.getRandomNumber(6);
        PhoneCode phoneCode = new PhoneCode();
        phoneCode.setId(RandomUtil.getUUID());
        phoneCode.setMobile(mobile);
        phoneCode.setCode(code);
        phoneCode.setCreateTime(System.currentTimeMillis());
        phoneCodeDao.add(phoneCode);
        SmsUtil.sendSms(mobile,code);
    }

    public void verifyMobileCode(String mobile,String code) throws Exception {
        PhoneCode phoneCode = phoneCodeDao.getByMobile(mobile);
        if(phoneCode == null){
            throw new Exception("未收到验证码? 请重试");
        }
        if(!code.equals(phoneCode.getCode())){
            throw new Exception("验证码不正确");
        }
    }
}
